package src.dp.stock;

public class no714 {//买卖股票含手续费

    public int maxProfit(int[] prices, int fee) {
        int len = prices.length;
        int[][] dp = new int[len][2];

        dp[0][0] = 0;  //不持有
        dp[0][1] = -prices[0];  //持有股票

        for (int i = 1; i < len; i++) {
            //含手续费时只需在卖出时减掉fee即可
            dp[i][0] = Math.max(dp[i - 1][0], dp[i - 1][1] + prices[i] - fee);
            dp[i][1] = Math.max(dp[i - 1][1], dp[i - 1][0] - prices[i]);
        }
        return dp[len - 1][0];

    }

    public int maxProfit2(int[] prices, int fee) {
        int len = prices.length;
        int[][] dp = new int[len][2];

        dp[0][0] = 0;  //不持有
        dp[0][1] = -prices[0] - fee;  //持有股票

        for (int i = 1; i < len; i++) {
            dp[i][0] = Math.max(dp[i - 1][0], dp[i - 1][1] + prices[i]);
            //如果在买入股票是就 -fee 也可以，但注意初始化dp[0][1]时就要也 -fee
            dp[i][1] = Math.max(dp[i - 1][1], dp[i - 1][0] - prices[i] - fee);
        }
        return dp[len - 1][0];
    }
}
